package com.devin.service.Impl;

import com.devin.dao.MultiMapper;
import com.devin.dao.OrderMapper;
import com.devin.dao.ProductMapper;
import com.devin.pojo.*;
import com.devin.service.OrderService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private MultiMapper multiMapper;

    @Override
    public List<Order> getOrders(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Order> orders = orderMapper.getOrders(page, size);
        /*避免侵入式加载，打印时立即加载*/
        for(Order each:orders){
            each.getId();
        }
        return orders;
    }

    @Override
    public List<Product> toAddOrder() {
        return productMapper.getProducts();
    }

    @Override
    public void addOrder(OrderAndTraveller orderAndTraveller) {
        Member member = orderAndTraveller.getMember();
        orderMapper.addMember(member);

        Order order = orderAndTraveller.getOrder();
        order.setMemberId(member.getId());
        orderMapper.addOrder(order);

        List<Traveller> travellers = orderAndTraveller.getTravellers();
        multiMapper.addTravellers(travellers);
        multiMapper.addOrderAndTravellers(order.getId(),travellers);
    }

    @Override
    public Order getOrderInfo(Integer orderId){
        Order order = orderMapper.getOrderInfo(orderId);
        /*避免侵入式加载，打印时立即加载*/
        System.out.println(order.getId());
        return order;
    }

    @Override
    public void delOrder(Integer orderId, Integer memberId) {
        orderMapper.delMember(memberId);
        orderMapper.delTravellers(orderId);
        orderMapper.delOrderAndTravellers(orderId);
        orderMapper.delOrder(orderId);
    }

    @Override   
    public void delOrdersChecked(Integer[] ids) {
        multiMapper.delMemberByOrderId(ids);
        multiMapper.delTravellers(ids);
        multiMapper.delOrdersAndTravellers(ids);
        multiMapper.delOrdersChecked(ids);
    }
}
